package com.example.studentmanagersystem;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * @version 1.0
 * @author cwf
 * data 2015-11-12
 *
 */
public class StudentMainActivity extends Activity implements OnClickListener {

	private Button updatePersonal,selectClasses,searchGrade;
	private Button actionBack,actionMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.student_main_activity);
		
		this.init();
		
	}
	
	/**
	 * initialize widget
	 */
	private void init() {
		
		this.updatePersonal=(Button) this.findViewById(R.id.student_main_personal_button);
		this.selectClasses=(Button) this.findViewById(R.id.student_main_selectClass_button);
		this.searchGrade=(Button) this.findViewById(R.id.student_main_searchGrade_button);
		
		this.actionBack=(Button) this.findViewById(R.id.stuMain_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.stuMain_action_menu);
		
		this.updatePersonal.setOnClickListener(this);
		this.selectClasses.setOnClickListener(this);
		this.searchGrade.setOnClickListener(this);
		
		this.actionBack.setOnClickListener(this);
		this.actionMenu.setOnClickListener(this);
		
	}
	

	/**
	 * Listener
	 */
	@Override
	public void onClick(View v) {
		 
		Intent intent=null;
		int id=v.getId();
		if(id==R.id.student_main_personal_button){
			
			
			intent=new Intent(this,StudentInfoChangeActivity.class);
			
			startActivity(intent);
			
		}else if(id==R.id.student_main_selectClass_button){
			
			
			intent=new Intent(this,StudentSelectClassesActivity.class);
			
			startActivity(intent);
			
		}else if(id==R.id.student_main_searchGrade_button){
			
			
			intent=new Intent(this,StudentSearchGradeActivity.class);
			
			startActivity(intent);
			
		}else if(id==R.id.stuMain_action_backButton){
			
			exit();
			
		}else if(id==R.id.stuMain_action_menu){
			
			PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v,this);
			popupMenuUtil.actionBackListener();
			
		}
		
	}
/**
 * click back button,the program will exit	
 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		
		 if (keyCode == KeyEvent.KEYCODE_BACK) {  
	            exit();  
	            return false;  
	        } else {  
	            return super.onKeyDown(keyCode, event);  
	        }  
	}

	boolean isExit;
	private void exit() {
		 
		if (!isExit) {  
            isExit = true;  
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();  
            mHandler.sendEmptyMessageDelayed(0, 2000);  
        } else {  
            Intent intent = new Intent(Intent.ACTION_MAIN);  
            intent.addCategory(Intent.CATEGORY_HOME);  
            startActivity(intent);  
            this.finish();  
        }  
		
	}
	
	Handler mHandler = new Handler() {  
		  
        @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  
            isExit = false;  
        }  
  
    }; 
	
	
}
